package com.hexiang.spring.dao;

import com.hexiang.dao.OmsOrderDao;
import com.hexiang.domain.OmsOrder;
import com.hexiang.exception.MyDaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.*;
import java.util.List;

@Repository
@Slf4j
public class JdbcTemplateOmsOrderDaoImpl implements OmsOrderDao {

    @Autowired
    private DataSource dataSource;

    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    private void postConstruct() {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }


    @Override
    public Long save(OmsOrder omsOrder) {
        KeyHolder keyHolder = new GeneratedKeyHolder();

        String sql = "INSERT INTO t_oms_order (member_id, order_sn, create_time, member_username, total_amount, pay_amount, freight_amount, pay_type, source_type, status, order_type) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

        jdbcTemplate.update(conn -> {
            PreparedStatement ps = null;
            try {
                ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
                ps.setLong(1, omsOrder.getMemberId());
                ps.setString(2, omsOrder.getOrderSn());
                ps.setDate(3, new Date(omsOrder.getCreateTime().getTime()));
                ps.setString(4, omsOrder.getMemberUsername());
                ps.setBigDecimal(5, omsOrder.getTotalAmount());
                ps.setBigDecimal(6, omsOrder.getPayAmount());
                ps.setBigDecimal(7, omsOrder.getFreightAmount());
                ps.setInt(8, omsOrder.getPayType());
                ps.setInt(9, omsOrder.getSourceType());
                ps.setInt(10, omsOrder.getStatus());
                ps.setInt(11, omsOrder.getOrderType());
            } catch (SQLException e) {
                log.error("saveOmsOrderErr", e);
                throw new MyDaoException(e);
            }
            return ps;
        }, keyHolder);
        return keyHolder.getKey().longValue();
    }

    @Override
    public OmsOrder get(long id) {
        return jdbcTemplate.queryForObject("select *  from t_oms_order where id = ?", new Object[]{id}, OmsOrder.class);
    }

    @Override
    public Integer deleteById(long id) {
        return jdbcTemplate.update("delete from t_oms_order where id = ?", id);
    }


    @Override
    public Integer update(OmsOrder omsOrder) {
        String sql = "UPDATE t_oms_order SET member_id=?, order_sn=?, member_username=?, total_amount=?, pay_amount=?, freight_amount=?, pay_type=?, source_type=?, status=?, order_type=?, modify_time=now() WHERE id=? ;";
        return jdbcTemplate.update(sql, omsOrder.getMemberId(), omsOrder.getOrderSn(),
                omsOrder.getTotalAmount(), omsOrder.getPayAmount()
                , omsOrder.getFreightAmount(), omsOrder.getPayType(), omsOrder.getSourceType()
                , omsOrder.getStatus(), omsOrder.getOrderType(), omsOrder.getId());
    }

    @Override
    public List<OmsOrder> queryAll() {
        return jdbcTemplate.query("select * from t_oms_order", (resultSet, i) -> toOmsOrder(resultSet));
    }

    @Override
    public List<OmsOrder> queryListByLimit(int limit) {
        return jdbcTemplate.query("select * from t_oms_order limit ?", new Object[]{limit}, (resultSet, i) -> toOmsOrder(resultSet));
    }

    @Override
    public Integer count() {
        return jdbcTemplate.queryForObject("select count(*) from t_oms_order",
                Integer.class);
    }

    private OmsOrder toOmsOrder(ResultSet rs) throws SQLException {
        OmsOrder omsOrder = new OmsOrder();
        omsOrder.setId(rs.getLong("id"));
        omsOrder.setMemberId(rs.getLong("member_id"));
        omsOrder.setOrderSn(rs.getString("order_sn"));
        omsOrder.setCreateTime(rs.getDate("create_time"));
        omsOrder.setMemberUsername(rs.getString("member_username"));
        omsOrder.setTotalAmount(rs.getBigDecimal("total_amount"));
        omsOrder.setPayAmount(rs.getBigDecimal("pay_amount"));
        omsOrder.setFreightAmount(rs.getBigDecimal("freight_amount"));
        omsOrder.setPayType(rs.getInt("pay_type"));
        omsOrder.setSourceType(rs.getInt("source_type"));
        omsOrder.setStatus(rs.getInt("status"));
        omsOrder.setOrderType(rs.getInt("order_type"));
        omsOrder.setModifyTime(rs.getDate("modify_time"));
        return omsOrder;
    }


    @Override
    public int[][] batchSave(List<OmsOrder> omsOrders) {
        String sql = "INSERT INTO t_oms_order (member_id, order_sn, create_time, member_username, total_amount, pay_amount, freight_amount, pay_type, source_type, status, order_type) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);";

        return jdbcTemplate.batchUpdate(sql, omsOrders, omsOrders.size(),

                (ps, omsOrder) -> {
                    ps.setLong(1, omsOrder.getMemberId());
                    ps.setString(2, omsOrder.getOrderSn());
                    ps.setDate(3, new java.sql.Date(omsOrder.getCreateTime().getTime()));
                    ps.setString(4, omsOrder.getMemberUsername());
                    ps.setBigDecimal(5, omsOrder.getTotalAmount());
                    ps.setBigDecimal(6, omsOrder.getPayAmount());
                    ps.setBigDecimal(7, omsOrder.getFreightAmount());
                    ps.setInt(8, omsOrder.getPayType());
                    ps.setInt(9, omsOrder.getSourceType());
                    ps.setInt(10, omsOrder.getStatus());
                    ps.setInt(11, omsOrder.getOrderType());
                });
    }

    @Override
    public int[][] batchUpdate(List<OmsOrder> tist) {
        return new int[0][];
    }
}